﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.Framework.Extension
{
    public static class Extensions
    {
        public static List<T> GetRandom<T>(this List<T> ListData, int Count)
        {
            if (ListData == null)
                return null;
            if (ListData.Count < 2)
                return ListData;

            Random rnd = new Random(DateTime.Now.Millisecond);
            List<T> retValue = ListData;

            for (int i = 0; i < retValue.Count; i++)
            {
                int rndValue = rnd.Next(retValue.Count);
                if (rndValue != i)
                {
                    T TmpValue = retValue[i];
                    retValue[i] = retValue[rndValue];
                    retValue[rndValue] = TmpValue;
                }
            }

            if (retValue.Count > Count)
                return retValue.GetRange(0, Count);
            return retValue;
        }
    }
}
